package org.msfj.admin.data.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.msfj.admin.data.service.DictIndexService;
import org.msfj.admin.model.DictIndex;
import org.msfj.admin.vo.DictIndexVo;
import org.msfj.admin.data.mapper.DictIndexMapper;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 系统数据字典索引信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "dictIndex")
public class DictIndexServiceImpl extends BaseServiceImpl<DictIndex> implements DictIndexService {

    @Autowired
    private DictIndexMapper dictIndexMapper;

    /**
     * 根据条件分页查询系统数据字典索引信息列表
     *
     * @param dicIndexVo
     * @param page 当前第几页
     * @param rows 每页多少条记录
     * @return PageInfo<DictIndex>
     */
    @Override
    //@Cacheable
    public PageInfo<DictIndexVo> getByDicIndex(DictIndexVo dicIndexVo, int page, int rows){
        Example example = new Example(DictIndex.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode()).andLike("name","%"+dicIndexVo.getName()+"%");
        PageHelper.startPage(page, rows);
        List<DictIndex> dictIndexList = super.getByExample(example);
        PageInfo dicIndexVoPageInfo = new PageInfo(dictIndexList);
        dicIndexVoPageInfo.setList(setOtherProperties(dictIndexList));
        return dicIndexVoPageInfo;
    }

    /**
     * 根据条件查询所有系统数据字典索引信息对象列表
     *
     * @param dicIndexVo
     * @return List<DictIndex>
     */
    @Override
    //@Cacheable
    public List<DictIndexVo> getAllDicIndex(DictIndexVo dicIndexVo){
        Example example = new Example(DictIndex.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode()).andLike("name","%"+dicIndexVo.getName()+"%");
        List<DictIndexVo> dicIndexVoList = setOtherProperties(super.getByExample(example));
        return dicIndexVoList;
    }

    @Override
    //@Cacheable
    public List<DictIndexVo> getDictIndexByKey(String key) {
        Example example = new Example(DictIndex.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode()).andEqualTo("key",key);
        List<DictIndexVo> dicIndexList = setOtherProperties(super.getByExample(example));
        return dicIndexList;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param dictIndex
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public DictIndexVo setOtherProperties(DictIndex dictIndex) {
        DictIndexVo dicIndexVo = new DictIndexVo();
        BeanUtils.copyProperties(dictIndex, dicIndexVo);
        return dicIndexVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param dicIndices
     * @return List<DictIndexVo>
     */
    @Override
    //@Cacheable
    public List<DictIndexVo> setOtherProperties(List<DictIndex> dicIndices) {
        List<DictIndexVo> dicIndexVos = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(dicIndices)) {
            for(DictIndex dictIndex : dicIndices) {
                DictIndexVo dicIndexVo = new DictIndexVo();
                BeanUtils.copyProperties(dictIndex, dicIndexVo);
                dicIndexVos.add(dicIndexVo);
            }
        }
        return dicIndexVos;
    }

    @Override
    //@Cacheable
    public List<Map<String, Object>> getAllDict() {
        List<Map<String,String>> list = dictIndexMapper.getAllDict();
        List<Map<String,Object>> result = CollectionUtil.newArrayList();
        if(!list.isEmpty()){
            for (Map<String,String> m:list) {
                if(m.get("type").equals("0")){
                    Map<String,Object> code = CollectionUtil.newHashMap();
                    code.put("name",m.get("code"));
                    List<Map<String,Object>> list1 = CollectionUtil.newArrayList();
                    for (Map<String,String> m1:list) {
                        if(m1.get("type").equals("1")&&m1.get("code").startsWith(m.get("code"))){
                            Map<String,Object> code1 = CollectionUtil.newHashMap();
                            code1.put("code",m1.get("code"));
                            code1.put("value",m1.get("name"));
                            list1.add(code1);
                        }
                    }
                    code.put("value",list1);
                    result.add(code);
                }
            }
        }
        return result;
    }
}
